package pluginxmongo

import (
	"gitee.com/douhun/easy-ngo/clients/xmongo"
)

var dbClients map[string]*xmongo.Client

func set(key string, client *xmongo.Client) {
	if dbClients == nil {
		dbClients = make(map[string]*xmongo.Client, 1)
	}
	dbClients[key] = client
}

func GetDBClientByKey(key string) (cli *xmongo.Client) {
	var ok bool
	cli, ok = dbClients[key]
	if !ok {
		return nil
	}
	return cli
}

func GetDBClient() (cli *xmongo.Client) {
	return GetDBClientByKey("default")
}

func GetDBByKey(key string) (db *xmongo.Database) {
	client := GetDBClientByKey(key)
	databaseName := client.Config.DatabaseName
	mongodb := client.Database(databaseName)
	xmongodb := &xmongo.Database{
		mongodb,
	}
	return xmongodb
}

func GetDB() (db *xmongo.Database) {
	return GetDBByKey("default")
}
